<template>
  <div id="Payment3">
    <h1>支付金额：<span style="font-size: 30px;color: red">￥{{orderTotal}}</span> </h1>
    <div style="text-align: left">
      <span>你正在使用账号余额支付：</span>
    </div>
    账号密码：<el-input v-model="password1" style="width: 500px" placeholder="请输入账号密码"></el-input><br>
    <el-button type="primary" @click="Zf()" style="margin-top: 20px">支付</el-button>

  </div>

</template>
<script>

export default {
  name:"Payment3",
  data() {
    return {
      orderTotal:0,
      //账号密码（验证用的）
      password:"",
      //账号id
      uid:this.$root.uid,
      //账号密码
      password1:"",
      //商户
      business:{},
      //供应商
      vendor:{},
      //支付状态
      PaymentStatus:"",
      //选中的购物车项（为了支付后的购物车删除）
      tableData:this.$root.shoppingCarts,
      //选中的订单（为了支付后的订单状态修改）
      order:this.$root.order,

    }
  },
  methods:{
    //查询账号密码
    select(){
     this.$axios.get(`/zhuche/One/${this.uid}`).then(r=>{
       this.password=r.data.password
       console.log("this.password",this.password)
       if(r.data.isBusinesses===1){
         this.$axios.get(`Businesses/selectById1/${this.uid}`).then(r=>{
          this.business=r.data
           console.log("this.business",r.data)
         })
       }
       if(r.data.isVendor===1){
         this.$axios.get(`vendor/selectById?userId=${this.uid}`).then(r=>{
           this.vendor=r.data
           console.log("this.vendor",r.data)
         })
       }
     })
    },
    //支付
    Zf(){
      if(this.password1===this.password){
        if(this.business.balance>=this.orderTotal){
       //商户
          let flag=confirm("余额可用，确定用余额支付吗")
          if(flag){
            this.business.balance=this.business.balance-this.orderTotal
            console.log("商户余额",this.business.balance)
            //支付
            this.$axios.put(`Businesses/update`,this.business).then(r=>{
              //支付状态
              if(r.data==="ok"){
                //支付成功后，订单状态的修改         //供应商的还没写
                this.order.status=0
                this.$axios.put(`Order/update`,this.order).then(m=>{
                  this.PaymentStatus=m.data
                  if(m.data==='ok'){
                    this.$message({
                      type:"success",
                      message:"支付成功"
                    });
                    if(this.tableData!=null || this.tableData!==undefined){
                      // 支付成功后，购物车删除
                      for(let i=0;i<this.tableData.length;i++){
                        console.log("单个购物项",this.tableData[i])
                        let cartId=this.tableData[i].cartId
                        console.log("单个购物项id",cartId)
                        this.$axios.delete(`ShoppingCart/${cartId}`).then(r=>{
                          console.log(r.data)
                        })
                      }
                    }

                    // 路由跳转，跳转到订单确认页面
                    this.$router.push({name:`PaymentStatus`,params:{PaymentStatus:this.PaymentStatus}})
                  }
                })

              }else {
                //多此一举
                this.$message({
                  type: "no",
                  message: "支付失败"
                });
              }
            })
          }
        }else if (this.vendor.balance>=this.orderTotal){
      //供应商
          let flag=confirm("余额可用，确定用余额支付吗")
          if(flag){
            this.vendor.balance=this.vendor.balance-this.orderTotal
            console.log("供应商余额",this.vendor.balance)
            this.$axios.put(`vendor/update`,this.vendor).then(r=>{
              //支付状态
              this.PaymentStatus=r.data
                if(r.data==="ok"){
                  //支付成功后，订单状态的修改
                  this.order.status=0
                  this.$axios.put(`Order/update`,this.order).then(m=>{
                    this.PaymentStatus=m.data
                    if(m.data==='ok'){
                      this.$message({
                        type:"success",
                        message:"支付成功"
                      });
                      if(this.tableData!=null){
                        // 支付成功后，购物车删除
                        for(let i=0;i<this.tableData.length;i++){
                          let cartId=this.tableData[i].cartId
                          console.log("单个购物项id",cartId)
                          this.$axios.delete(`ShoppingCart/${cartId}`).then(r=>{
                            console.log(r.data)
                          })
                        }
                      }
                      // 路由跳转，跳转到订单确认页面
                      this.$router.push({name:`PaymentStatus`,params:{PaymentStatus:this.PaymentStatus}})
                    }
                  })

                }else {
                  //多此一举
                  this.$message({
                    type: "no",
                    message: "支付失败"
                  });
                }
            })
          }
        }else{
          this.$message({
            type:"error",
            message:"余额不足"
          });
        }
      }else {
        this.$message({
          type:"error",
          message:"密码错误"
        });
        this.password1=""
      }
    }
  },
  created() {
    this.orderTotal=this.$root.orderTotal
    console.log("订单总数",this.orderTotal)
    console.log("订单",this.order)
    console.log("购物车",this.tableData)
    this.select()
  }
}
</script>

<style scoped>

</style>